package com.effectiv.gooruda.rest.api.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

import com.effectiv.gooruda.rest.api.domain.Session;

import com.effectiv.gooruda.rest.api.operation.SessionOperations;

@Controller
@RequestMapping("/api/session")
public class SessionContoller {

	@Autowired
	private SessionOperations operations;

	@RequestMapping(value = "/getattributes/{sessionId}", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, String> getAttributes(@PathVariable String sessionId) {
		return operations.getAttributes(sessionId);
	}

	@RequestMapping(value = "/getattribute/{sessionId}/{key}", method = RequestMethod.GET)
	public @ResponseBody
	Object getAttribute(@PathVariable("sessionId") String sessionId,
			@PathVariable("key") String key) {
		return operations.getAttribute(sessionId, key);
	}

	@RequestMapping(value = "/findsessionbyuser/{userName}", method = RequestMethod.GET)
	public @ResponseBody
	Session findSessionByUser(@PathVariable String userName) {
		return operations.findSessionByUser(userName);
	}

	@RequestMapping(method = RequestMethod.POST)
	public @ResponseBody
	Session create(@PathVariable("userName") String userName) {
		return operations.create(userName);
	}

	@RequestMapping(method = RequestMethod.DELETE)
	public void invalidate(@PathVariable("sessionId") String sessionId) {
		operations.invalidate(sessionId);
	}

	@RequestMapping(value = "/removeallattributes/{sessionId}", method = RequestMethod.DELETE)
	public @ResponseBody
	Session removeAllAttributes(String sessionId) {
		return operations.removeAllAttributes(sessionId);
	}

	@RequestMapping(value = "/removeattribute/{sessionId}/{key}", method = RequestMethod.DELETE)
	public @ResponseBody
	Session removeAttribute(String sessionId, String key) {
		return operations.removeAttribute(sessionId, key);
	}

	@RequestMapping(value = "/setattribute/{sessionId}/{key}/{value}", method = RequestMethod.DELETE)
	public @ResponseBody
	Session setAttribute(@PathVariable("sessionId") String sessionId,
			@PathVariable("key") String key, @PathVariable("value") String value) {
		return operations.setAttribute(sessionId, key, value);
	}

}
